package one.stand.service.sys;

import one.stand.advice.ResultEnum;
import one.stand.mapper.ArticleMapperExt;
import one.stand.mapper.ArticleTypeMapper;
import one.stand.model.ArticleType;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author cjr
 */
@Service
public class ArticleTypeServiceImpl implements ArticleTypeService {
    @Autowired
    private ArticleTypeMapper articleTypeMapper;
    @Autowired
    private ArticleMapperExt articleMapperExt;

    @Override
    public void save(ArticleType articleType) {
        AssertUtil.nonNull(articleType.getName(), ResultEnum.PARAM_CHECK, "分类名称不能为空");
        if (Objects.nonNull(articleType.getId())) {
            articleTypeMapper.updateByPrimaryKeySelective(articleType);
        } else {
            articleTypeMapper.insertSelective(articleType);
        }
    }

    @Override
    public void delete(Integer id) {
        Integer count = articleMapperExt.selectTypeCount(id);
        AssertUtil.isTrue(count <= 0, ResultEnum.PARAM_CHECK, "分类下存在文章，不能删除");
        articleTypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ArticleType get(Integer id) {
        return articleTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer count(Integer id) {
        return articleMapperExt.selectTypeCount(id);
    }
}
